package com.wlyuan.open.domain;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.time.LocalDateTime;

@Data
public class PushTask {
    @ApiModelProperty("主键id")
    private PushTaskId id;

    private Long orderId;

    private String contractNo;
    private PushBusinessType businessType;
    private PushType pushType;
    private String pushStrategy;
    private Long tenantId;
    private PushTaskStatus status;
    private String remark;
    private String failReason;
    private Integer retryCount;
    private LocalDateTime nextPushAt;
    private LocalDateTime finishAt;
    private LocalDateTime createAt;
    private JSONObject businessInfo;
    private String requestBody;
    private String responseBody;
    private String requestUrl;

    public PushTask success(String requestUrl,String requestBody,String responseBody){
        this.status = PushTaskStatus.SUCCEED;
        this.finishAt = LocalDateTime.now();
        this.requestBody = requestBody;
        this.responseBody = responseBody;
        this.requestUrl = requestUrl;
        return this;
    }

    public PushTask fail(String requestUrl,String requestBody,String responseBody,String failReason){
        this.status = PushTaskStatus.FAILED;
        this.finishAt = LocalDateTime.now();
        this.requestBody = requestBody;
        this.responseBody = responseBody;
        this.failReason = failReason;
        //如果重试次数小于3，则状态更改为重试
        if(this.retryCount!=null && this.retryCount<3){
            this.status = PushTaskStatus.CREATED;
            this.retryCount = retryCount+1;
        }else{
            this.requestUrl = requestUrl;
        }
        return this;
    }



}
